<template>
  <div>
    <el-form :inline="true"
             class="demo-form-inline">
      <el-form-item>
        <el-button type="primary"
                   v-if="hasAuth('sys:menu:save')"
                   @click="addMenu">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData"
              style="width: 100%;margin-bottom: 20px;"
              row-key="id"
              border
              stripe
              default-expand-all
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}">

      <el-table-column prop="name"
                       label="名称"
                       width="180">
      </el-table-column>
      <el-table-column prop="orderNum"
                       label="排序"
                       width="50"
                       align="center">
      </el-table-column>
      <el-table-column prop="perms"
                       label="权限编码"
                       width="180">
      </el-table-column>
      <el-table-column prop="icon"
                       label="图标">
      </el-table-column>
      <el-table-column prop="type"
                       label="类型"
                       width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type === 0"
                  size="small">目录</el-tag>
          <el-tag v-else-if="scope.row.type === 1"
                  size="small"
                  type="success">菜单</el-tag>
          <el-tag v-else-if="scope.row.type === 2"
                  size="small"
                  type="info">按钮</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="path"
                       label="菜单URL"> </el-table-column>
      <el-table-column prop="component"
                       label="菜单组件"> </el-table-column>
      <el-table-column prop="orderNum"
                       label="排序号"> </el-table-column>
      <el-table-column prop="statu"
                       label="状态"
                       width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.statu === 0"
                  size="small"
                  type="danger">禁用</el-tag>
          <el-tag v-else-if="scope.row.statu === 1"
                  size="small"
                  type="success">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作"
                       width="120"> <template slot-scope="scope">
          <el-button type="text"
                     @click="editHandle(scope.row.id)"
                     v-if="hasAuth('sys:menu:update')">编辑</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm title="确定要删除这条记录吗？"
                         @confirm="delHandle(scope.row.id)">
            <el-button type="text"
                       v-if="hasAuth('sys:menu:delete')"
                       slot="reference">删除</el-button>
          </el-popconfirm>
        </template> </el-table-column>
    </el-table>

    <el-dialog title="提示"
               :visible.sync="dialogVisible"
               width="30%"
               :before-close="handleClose">
      <el-form :model="editForm"
               ref="editForm"
               :rules="editFormRules"
               label-width="100px"
               class="demo-editForm">
        <el-form-item label="上级菜单"
                      prop="parentId">
          <el-select v-model="editForm.parentId"
                     placeholder="上级菜单">
            <template v-for="(item,index) in tableData">
              <el-option :key="index"
                         :label="item.name"
                         :value="item.id">
              </el-option>
              <template v-for="child in item.children">
                <el-option :key="child.id"
                           :value="child.id"
                           :label="child.name">
                  <span>{{'- ' + child.name}}</span>
                </el-option>
              </template>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单名称"
                      prop="name"
                      label-width="100px">
          <el-input v-model="editForm.name"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限编码"
                      prop="perms"
                      label-width="100px">
          <el-input v-model="editForm.perms"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标"
                      prop="icon"
                      label-width="100px">
          <el-input v-model="editForm.icon"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单URL"
                      prop="path"
                      label-width="100px">
          <el-input v-model="editForm.path"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单组件"
                      prop="component"
                      label-width="100px">
          <el-input v-model="editForm.component"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="类型"
                      prop="type"
                      label-width="100px">
          <el-radio-group v-model="editForm.type">
            <el-radio :label=0>目录</el-radio>
            <el-radio :label=1>菜单</el-radio>
            <el-radio :label=2>按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态"
                      prop="statu"
                      label-width="100px">
          <el-radio-group v-model="editForm.statu">
            <el-radio :label=0>禁用</el-radio>
            <el-radio :label=1>正常</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序号"
                      prop="orderNum"
                      label-width="100px">
          <el-input-number v-model="editForm.orderNum"
                           :min="1"
                           label="排序号">1</el-input-number>
        </el-form-item>
      </el-form>
      <el-button type="primary"
                 @click="submitForm">立即创建</el-button>
      <el-button @click="resetForm">重置</el-button>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'Menu',
  components: {},
  props: {},
  data() {
    return {
      dialogVisible: false,
      tableData: [],
      editForm: {},
      editFormRules: {
        parentId: [
          { required: true, message: '请选择上级菜单', trigger: 'blur' },
        ],
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        perms: [{ required: true, message: '请输入权限编码', trigger: 'blur' }],
        type: [{ required: true, message: '请选择状态', trigger: 'blur' }],
        orderNum: [
          { required: true, message: '请填入排序号', trigger: 'blur' },
        ],
        statu: [{ required: true, message: '请选择状态', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.getMenu()
  },
  methods: {
    getMenu() {
      this.$axios.get('/sys/menu/list').then((res) => {
        this.tableData = res.data.data
      })
    },
    addMenu() {
      this.dialogVisible = true
    },
    handleClose() {
      this.resetForm()
      this.dialogVisible = false
      this.getMenu()
    },

    resetForm() {
      this.$refs['editForm'].resetFields()
      this.editForm = {}
    },
    editHandle(id) {
      this.$axios.get('/sys/menu/info/' + id).then((res) => {
        this.editForm = res.data.data
        this.dialogVisible = true
      })
    },
    submitForm() {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          let flag = this.editForm.id ? 'edit' : 'save'
          console.log(flag)
          this.$axios.post('/sys/menu/' + flag, this.editForm).then((res) => {
            console.log(res)
            this.handleClose()
            this.$message({
              showClose: true,
              message: '更新成功',
              type: 'success',
              onClose: () => {},
            })
          })
        } else {
          return false
        }
      })
    },
    delHandle(id) {
      this.$axios.delete('/sys/menu/remove/' + id).then((res) => {
        this.$message.success('删除成功')
        this.getMenu()
      })
    },
  },
  watch: {},
  computed: {},
  mounted() {},
}
</script>
<style scoped>
</style>